package com.bootcamp.mall.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @program: mall
 * @description: make sure one factory
 * @author: Keyu Li
 * @create: 2021-11-09 09:15
 **/

public class MyBatisUtil {
    private static SqlSessionFactory factory;

    static {
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        // 通过类加载器获取mybatis-config.xml配置文件
        System.out.println("mybatis类加载器：\n"+MyBatisUtil.class.getClassLoader());
        InputStream stream = MyBatisUtil.class.getClassLoader().getResourceAsStream("config/mybatis-config.xml");
        factory = factoryBuilder.build(stream);
    }

    public static SqlSession getSqlsession(){
        return factory.openSession();
    }
}
